package com.cci.kangdao.controller;

import com.cci.kangdao.service.AKeyLoginService;
import com.cci.kangdao.utilTool.ParameterTool;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author linjiankun
 * @date 2022/6/10 9:40
 */

@Controller
public class AKeyLoginController {

    private Logger log = Logger.getLogger(this.getClass().getName());

    @Autowired
    private AKeyLoginService aKeyLoginService;


    /**
     * 一键登录和手机号验证码登录接口
     * @param paramStr
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/aKeyLogin.aspx", method= RequestMethod.POST)
    public ModelAndView aKeyLogin(@RequestBody String paramStr, HttpServletRequest request,
                                                   HttpServletResponse response ) {
        try{
            return aKeyLoginService.aKeyLogin(paramStr,request,response);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            String msg = "{\"status\": 1,\"msg\": \"系统异常\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }



    /**
     * 通过手机号发送验证码   应用于手机号码验证码登录使用
     * @param
     * @return
     */
    @RequestMapping("/aKeyLoginSendVerifyCode.aspx")
    public ModelAndView aKeyLoginSendVerifyCode(HttpServletRequest request,
                                          HttpServletResponse response) throws Exception {

        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            return aKeyLoginService.aKeyLoginSendVerifyCode(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }




    /**
     * 一键登录或他人号码验证登录用户刷新local token接口
     * @param
     * @return
     */
    @RequestMapping("/refreshLocalToken.aspx")
    public ModelAndView refreshLocalToken(HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {

        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            return aKeyLoginService.refreshLocalToken(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }



    /**
     * 一键登录或手机号验证码登录前校验身份接口  判断是否是内部用户
     * @param
     * @return
     */
    @RequestMapping("/aKeyLoginVerifyIdentity.aspx")
    public ModelAndView aKeyLoginVerifyIdentity(HttpServletRequest request,
                                                HttpServletResponse response) throws Exception {

        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            return aKeyLoginService.aKeyLoginVerifyIdentity(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }


    /**
     * 完善账户信息接口（一键登录和手机号验证码登录的用户，如果是新注册的并且没有完善信息的用户，需要完善账户信息）
     * @param paramStr
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/perfectAccountMessage.aspx", method= RequestMethod.POST)
    public ModelAndView perfectAccountMessage(@RequestBody String paramStr, HttpServletRequest request,
                                  HttpServletResponse response ) {
        try{
            return aKeyLoginService.perfectAccountMessage(paramStr,request,response);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
            String msg = "{\"status\": 1,\"msg\": \"系统异常\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }



    /**
     * 主动服务的服务站列表
     * @param
     * @return
     */
    @RequestMapping("/getWhiteList.aspx")
    public ModelAndView getWhiteList(HttpServletRequest request,
                                                HttpServletResponse response) throws Exception {

        try{
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            return aKeyLoginService.getWhiteList(map);
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }
}
